package beans;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import model.Teren;
import conversions.Conversions;

/**
 * Session Bean implementation class TerenDAO
 */
@Stateless
public class TerenDAO implements TerenDAORemote {

	@PersistenceContext
	EntityManager em;

	/**
	 * Default constructor.
	 */
	public TerenDAO() {
		// TODO Auto-generated constructor stub
	}

	public void insertTeren(TerenDTO teren) {
		Teren pr = new Conversions().fromDTO(teren);

		// pr.setAdresa(prop.getAdresa());
		em.persist(pr);
	}
	
	@Override
	public List<TerenDTO> getTerenList() {
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Teren> cq = cb.createQuery(Teren.class);
		Root<Teren> prop = cq.from(Teren.class);

		// cq.select(casa).where(cb.ge(casa.get(Casa_.pret),pret));

		cq.select(prop);

		TypedQuery<Teren> q = em.createQuery(cq);
		List<Teren> props = q.getResultList();

		List<TerenDTO> propsDTO = new ArrayList<>();
		for (Teren p : props) {
			propsDTO.add(new Conversions().toDTO(p));
		}
		return propsDTO;
	}
}
